package com.huawei.bitfun.insight.utils;

import com.huawei.bitfun.insight.InsightExContext;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.fileChooser.FileChooserDialog;
import com.intellij.openapi.fileChooser.FileChooserFactory;
import com.intellij.openapi.fileChooser.FileSaverDescriptor;
import com.intellij.openapi.fileChooser.FileSaverDialog;
import com.intellij.openapi.fileChooser.ex.FileSaverDialogImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.vfs.VirtualFile;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DialogManager {
    private static final String VALID_FILE_NAME_ERROR_MESSAGE = "Make sure the file name doesn't contain" +
            " the following characters: * : ? \" \\&lt; > |";

    private static final String FILE_NAME_TOO_LONG_ERROR_MESSAGE = "Make sure that the file name" +
            " does not exceed 238 characters.";

    private static final String INVALID_PATH_ERROR_MESSAGE = "Make sure that the path is valid: ";

    public static FileSaverDialog getSaveFileDialog(FileSaverDescriptor saverDescriptor) {
        Project project = InsightExContext.getProject();
        FileSaverDialog dialog;
        if (SystemInfo.isMac) {
            dialog = FileChooserFactory.getInstance().createSaveFileDialog(saverDescriptor, project);
        } else {
            dialog = new FileSaverDialogImpl(saverDescriptor, project) {

                @Override
                protected void doOKAction() {
                    final File file = getFile();
                    String fileNameWithoutExtension = FilenameUtils.getBaseName(file.getName());
                    String regex = "^[^*:?\"<>|]*$";
                    Pattern pattern = Pattern.compile(regex);
                    Matcher matcher = pattern.matcher(fileNameWithoutExtension);
                    if (!matcher.matches()) {
                        Messages.showErrorDialog(getContentPane(), VALID_FILE_NAME_ERROR_MESSAGE, getTitle());
                    } else if (fileNameWithoutExtension.length() > 238) {
                        Messages.showErrorDialog(getContentPane(), FILE_NAME_TOO_LONG_ERROR_MESSAGE, getTitle());
                    } else if (!file.getParentFile().exists()) {
                        Messages.showErrorDialog(getContentPane(),
                                INVALID_PATH_ERROR_MESSAGE + file.getPath(), getTitle());
                    } else {
                        super.doOKAction();
                    }
                }
            };
        }
        return dialog;
    }

    public static String showDialogAndGetFile() {
        FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor();
        descriptor.setTitle("Select File");
        descriptor.setDescription("Select a file");
        descriptor.withFileFilter(virtualFile ->
                TraceFileType.getTraceFileType(virtualFile.getName()) != TraceFileType.DEFAULT_EXTENSION);
        Project project = InsightExContext.getProject();
        FileChooserDialog dialog = FileChooserFactory.getInstance().createFileChooser(descriptor,
                project, null);
        VirtualFile[] files = dialog.choose(project, VirtualFile.EMPTY_ARRAY);
        if (files.length == 0) {
            return "";
        }
        return files[0].getPath();
    }


}
